package com.softstao.guoyu.ui.activity.agent;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.agent.SubInfo;
import com.softstao.guoyu.model.me.Icon;
import com.softstao.guoyu.mvp.interactor.agent.AgentInteractor;
import com.softstao.guoyu.mvp.presenter.agent.AgentPresenter;
import com.softstao.guoyu.mvp.viewer.agent.SubInfoViewer;
import com.softstao.guoyu.ui.adapter.MeIconAdapter;
import com.softstao.softstaolibrary.library.utils.LZUtils;
import com.softstao.softstaolibrary.library.widget.CustomGridView;
import com.softstao.softstaolibrary.library.widget.CustomScrollerView;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/6/27.
 */

public class TeamMemberDetailActivity extends BaseActivity implements SubInfoViewer {

    @AIPresenter(presenter = AgentPresenter.class, interactor = AgentInteractor.class)
    AgentPresenter presenter;

    @BindView(R.id.bottom)
    LinearLayout bottom;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.level)
    TextView level;
    @BindView(R.id.pick_time)
    TextView pickTime;
    @BindView(R.id.balance)
    TextView balance;
    @BindView(R.id.icon_view)
    CustomGridView iconView;
    @BindView(R.id.scroll_view)
    CustomScrollerView scrollView;
    @BindView(R.id.total_num)
    TextView totalNum;
    @BindView(R.id.team_count)
    TextView teamCount;
    @BindView(R.id.referee_name)
    TextView refereeName;
    @BindView(R.id.join_date)
    TextView joinDate;
    @BindView(R.id.auth_date_view)
    TextView authDateView;
    @BindView(R.id.auth_date)
    TextView authDate;
    @BindView(R.id.late_order_date)
    TextView lateOrderDate;
    private MeIconAdapter meIconAdapter;
    private List<Icon> iconList = new ArrayList<>();
    private int[] imgList = {R.mipmap.team_ic, R.mipmap.order_ic, R.mipmap.me_icon_yeji};
    private String[] nameList = {"TA的团队", "相关订单", "查看业绩"};
    private int id,flag,agent_id;
    private SubInfo subInfo;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private String level_name,lateLy_date,join_date,auth_date;

    @Override
    public int _ContentView() {
        return R.layout.activity_team_member_detail;
    }

    @Override
    public void initView() {
        initTitle("团队成员详情");
        id = getIntent().getIntExtra("id", 0);
        flag = getIntent().getIntExtra("flag",0);
        if (flag==1){
            iconView.setVisibility(View.GONE);
            agent_id = getIntent().getIntExtra("agent_id",0);
        }

        addIcon();
        meIconAdapter = new MeIconAdapter(iconList);
        iconView.setAdapter(meIconAdapter);
        iconView.setOnItemClickListener((parent, view, position, id) ->
                Goto(iconList.get(position).getPic())
        );
    }

    private void initData() {
        name.setText(subInfo.getName());
        switch (subInfo.getLevel()){
            case 1:
                level_name = "天使";
                break;
            case 2:
                level_name = "皇冠";
                break;
            case 3:
                level_name = "一级";
                break;
            case 4:
                level_name = "市代";
                break;
            case 5:
                level_name = "省代";
                break;
            case 6:
                level_name = "总部";
                break;
            case 7:
                level_name = "未授权";
                break;
        }
        level.setText(level_name);
        authDateView.setText("升级为"+ level_name + "时间");
        lateLy_date = subInfo.getLatelyOrderDate()==0?"——":format.format(new Date(subInfo.getLatelyOrderDate()));
        pickTime.setText("最近提货："+lateLy_date);
        lateOrderDate.setText(lateLy_date);
        balance.setText("虚拟币："+ LZUtils.priceFormat(subInfo.getVirtualCurrency()) + "元");
        totalNum.setText(subInfo.getProductTotal()+"台");
        teamCount.setText(subInfo.getTeamCount()+"人");
        refereeName.setText(subInfo.getRefereeName());
        join_date = subInfo.getJoinDate()==0?"——":format.format(new Date(subInfo.getJoinDate()));
        joinDate.setText(join_date);
        auth_date = subInfo.getAuthDate()==0?"——":format.format(new Date(subInfo.getAuthDate()));
        authDate.setText(auth_date);
    }

    private void Goto(int position) {
        switch (position) {
            case R.mipmap.team_ic:
                Intent intent = new Intent(context,OtherTeamActivity.class);
                intent.putExtra("subInfo",subInfo);
                intent.putExtra("id",id);
                startActivity(intent);
                break;
            case R.mipmap.order_ic:
                Intent orderIntent = new Intent(context,AboutOrderActivity.class);
                orderIntent.putExtra("type","order");
                orderIntent.putExtra("id",id);
                startActivity(orderIntent);
                break;
            case R.mipmap.me_icon_yeji:
                Intent yejiIntent = new Intent(context,AboutOrderActivity.class);
                yejiIntent.putExtra("type","achieve");
                yejiIntent.putExtra("agent_id",id);
                startActivity(yejiIntent);
                break;
        }
    }

    private void addIcon() {
        for (int i = 0; i < imgList.length; i++) {
            Icon icon = new Icon();
            icon.setPic(imgList[i]);
            icon.setName(nameList[i]);
            iconList.add(icon);
        }
    }

    @OnClick(R.id.contact)
    public void onViewClicked() {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + subInfo.getMobile());
        intent.setData(data);
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getSubInfo();
    }

    @Override
    public void getSubInfo() {
        if (agent_id==0) {
            presenter.getSubInfo(SharePreferenceManager.getInstance().getAgentId(), id);
        }else {
            presenter.getSubInfo(agent_id,id);
        }
    }

    @Override
    public void getResult(SubInfo subInfo) {
        if (subInfo != null) {
            this.subInfo = subInfo;
            initData();
        }
    }
}
